package com.oncliedisk.administrator.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.oncliedisk.administrator.content.FileConfig;
import com.oncliedisk.administrator.model.EntryModel;
import com.oncliedisk.administrator.onlinedisk.R;
import com.oncliedisk.administrator.utils.FileUtils;
import com.oncliedisk.administrator.utils.ImageUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/12/3.
 */
public class FileAdapter extends BaseAdapter {
    private List<EntryModel> data;
    private Context context;
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    private ImageLoader imageLoad;
    private List<Integer> list_id = new ArrayList<Integer>();
    private ListView listView;

    public FileAdapter(List<EntryModel> data, Context context,ListView listView) {
        this.data = data;
        this.context = context;
        this.listView = listView;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoad = ImageUtils.getInstance(context).getImageLoader();
        options = ImageUtils.getListOptions();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("info", "调用一次");
        ViewHolder holder = null;
        final EntryModel model = data.get(position);
        String type = model.getMimeType();
        if (convertView != null && type.equals(FileConfig.TYPE_IMG) && type.equals(FileConfig.TYPE_PNG) && type.equals(FileConfig.TYPE_JPEG)) {

            holder = (ViewHolder) convertView.getTag();
        } else {

            convertView = inflater.inflate(R.layout.item_file, null);
            holder = new ViewHolder();
//            holder.img.set((ImageView) convertView.findViewById(R.id.file_img));
            holder.img = (ImageView) convertView.findViewById(R.id.file_img);
            holder.date = (TextView) convertView.findViewById(R.id.file_date);
            holder.name = (TextView) convertView.findViewById(R.id.file_name);
            holder.size = (TextView) convertView.findViewById(R.id.file_size);
            holder.check = (ImageView) convertView.findViewById(R.id.file_check);
            convertView.setTag(holder);
        }


        holder.size.setVisibility(View.GONE);
        holder.check.setVisibility(View.INVISIBLE);
        holder.name.setText(model.getFileName());

        //根据类型进行选择图标
        if (type.equals(FileConfig.TYPE_IMG) || type.equals(FileConfig.TYPE_PNG) || type.equals(FileConfig.TYPE_JPEG)) {
            //如果是图片
            if(!TextUtils.isEmpty(model.getDownPath())){
//                Log.i("info", "下载地址" + model.getDownPath());
//                holder.img.setTag(position);

                holder.img.setTag(model.getDownPath());


                imageLoad.displayImage(model.getDownPath(), holder.img, options, new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {

                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                        Log.i("model.getDownPath()", model.getDownPath()+"=加载图片=" + s);
                        ImageView img = (ImageView) listView.findViewWithTag(s);
                        if(bitmap != null && img != null){
                            img.setImageBitmap(bitmap);
                        }
                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
            }else{
                holder.img.setImageResource(R.drawable.loading);
            }
        } else if (type.equals(FileConfig.TYPE_FOLDER)) {
            holder.size.setVisibility(View.GONE);
            holder.img.setImageResource(R.drawable.folder);
        } else {
            holder.img.setImageResource(ImageUtils.getImageByType(model.getFileName()));
        }

        //判断是否有图片了，如果有图片了，就进行设置图片
        if (!type.equals(FileConfig.TYPE_FOLDER)) {

            holder.size.setVisibility(View.VISIBLE);
            holder.size.setText(model.getSize());
        }
        //设置日期
        holder.date.setText(FileUtils.getFormateTime(new Date(model.getModified())));
//        holder.date.setText(model.getModified());
        //如果是等待选中状态
        if (model.isLoading()) {
            holder.check.setVisibility(View.VISIBLE);
            holder.check.setImageResource(R.drawable.check);
        }
        //如果被选中
        if (model.isCheck()) {
            holder.check.setVisibility(View.VISIBLE);
            holder.check.setImageResource(R.drawable.check_blue);
        }
        //如果是正常状态
        if (model.isSingle() == true) {
            holder.check.setVisibility(View.INVISIBLE);
        }
        return convertView;

    }

    public class ViewHolder {
//        public final ThreadLocal<ImageView> img = new ThreadLocal<ImageView>();
        ImageView img;
        TextView name;
        TextView size;
        TextView date;
        ImageView check;
    }


    public List<EntryModel> getData() {
        return data;
    }


}
